package com.lagou.task02;

//玩家类
public class Player {

    //玩家姓名
    private String name;

    //玩家所持有的棋子
    private String chess;

    public Player() {
    }

    public Player(String name,String chess) {
        this.name = name;
        this.chess = chess;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getChess() {
        return chess;
    }

    public void setChess(String chess) {
        this.chess = chess;
    }

    //落棋子动作 返回值代表是否可以在该点落子 即有没有出界或者指定格子中是否已经有棋子
    public boolean chess(String[][] board, int x, int y){
        if (x <= 0 || y <= 0 || x > 16 || y > 16){
            System.out.println("无效的坐标，请重新落子");
            return false;
        }

        //检查原本此处有无棋子
        String s = board[x][y];
        if (s == "·"){
            //可以落子，进行播报
            show(x,y);
            return true;
        }else{
            //已有棋子在此处
            System.out.println("该坐标已经有棋子，请重新落子");
            return false;
        }
    }

    //步骤播报
    public void show(int x, int y){
        System.out.println(getName() + "在 x = " + (x-1) + "，y = " + (y-1) + "处落子");
    }


    //落子后检查自己是否获胜
    public boolean checkSelfWin(String[][] board, int x, int y){
        int up = upNum(board, x, y);
        int down = downNum(board, x, y);
        if (up + down == 4){
            //获胜
            System.out.println(getChess() + "纵五连珠，" + getName() + "获胜");
            System.out.println("胜负已分！");
            return true;
        }

        int left = leftNum(board, x, y);
        int right = rightNum(board, x, y);
        if (left + right == 4){
            System.out.println(getChess() + "横五连珠，" + getName() + "获胜");
            System.out.println("胜负已分！");
            return true;
        }

        int leftUp = leftUp(board, x, y);
        int rightDown = rightDown(board, x, y);
        if (leftUp + rightDown == 4){
            System.out.println(getChess() + "斜五连珠，" + getName() + "获胜");
            System.out.println("胜负已分！");
            return true;
        }

        int rightUp = rightUp(board, x, y);
        int leftDown = leftDown(board, x, y);
        if (rightUp + leftDown == 4){
            System.out.println(getChess() + "斜五连珠，" + getName() + "获胜");
            System.out.println("胜负已分！");
            return true;
        }

        return false;
    }

    //上连珠数
    public int upNum(String[][] board, int x, int y){
        if (x == 1){
            return 0;
        }
        int sum = 0;
        while (x >= 1 && y >= 1 && x <= 15 && y <= 15){
            if (board[--x][y] == getChess()){
                sum++;
            }else{
                break;
            }
        }
        return sum;
    }

    //下连珠数
    public int downNum(String[][] board, int x, int y){
        if (x == 16){
            return 0;
        }
        int sum = 0;
        while (x >= 1 && y >= 1 && x <= 15 && y <= 15){
            if (board[x][++y] == getChess()){
                sum++;
            }else{
                break;
            }
        }

        return sum;
    }

    //左连珠数
    public int leftNum(String[][] board, int x, int y){
        if (y == 1){
            return 0;
        }
        int sum = 0;
        while (x >= 1 && y >= 1 && x <= 15 && y <= 15){
            if (board[x][--y] == getChess()){
                sum++;
            }else{
                break;
            }
        }
        return sum;
    }

    //右连珠数
    public int rightNum(String[][] board, int x, int y){
        if (y == 16){
            return 0;
        }
        int sum = 0;
        while (x >= 1 && y >= 1 && x <= 15 && y <= 15){
            if (board[++x][y] == getChess()){
                sum++;
            }else{
                break;
            }
        }
        return sum;
    }

    //左上连珠数
    public int leftUp(String[][] board, int x, int y){
        if (x == 1 || y == 1){
            return 0;
        }
        int sum = 0;
        while (x >= 1 && y >= 1 && x <= 15 && y <= 15){
            if (board[--x][--y] == getChess()){
                sum++;
            }else{
                break;
            }
        }

        return sum;
    }


    //右上连珠数
    public int rightUp(String[][] board, int x, int y){
        if (x == 1 || y == 16){
            return 0;
        }
        int sum = 0;
        while (x >= 1 && y >= 1 && x <= 15 && y <= 15){
            if (board[--x][++y] == getChess()){
                sum++;
            }else{
                break;
            }
        }
        return sum;
    }


    //左下连珠数
    public int leftDown(String[][] board, int x, int y){
        if (x == 16 || y == 1){
            return 0;
        }
        int sum = 0;
        while (x >= 1 && y >= 1 && x <= 15 && y <= 15){
            if (board[++x][--y] == getChess()){
                sum++;
            }else{
                break;
            }
        }
        return sum;
    }

    //右下连珠数
    public int rightDown(String[][] board, int x, int y){
        if (x == 16 || y == 16){
            return 0;
        }
        int sum = 0;
        while (x >= 1 && y >= 1 && x <= 15 && y <= 15){
            if (board[++x][++y] == getChess()){
                sum++;
            }else{
                break;
            }
        }
        return sum;
    }
}
